package com.example.fragmentest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.fragmentest.Beam.User;
import com.example.fragmentest.fragment.CecondkFragment;
import com.example.fragmentest.fragment.FinstFragment;
import com.example.fragmentest.fragment.ThirdFragment;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {
private Button tofirst,tosecond,toshird;
private FinstFragment finstFragment=new FinstFragment();
private CecondkFragment cecondFragment=new CecondkFragment();
private ThirdFragment thirdFragment=new ThirdFragment();
private Fragment defFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceFragment(finstFragment);
        getUser();
        initView();
        setListener();

    }

    private void getUser() {
        List<User> userList= LitePal.findAll(User.class);
        if (userList.size()==0){
            User user=new User(0,"张三","123");
            User user1=new User(0,"李四","456");
            User user2=new User(0,"王五","789");
            user.save();
            user1.save();
            user2.save();

        }
    }

    private void setListener() {
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.tofirst:
                        initColor(v);
                        replaceFragment(finstFragment);
                    break;
                    case R.id.tosecond:
                        initColor(v);
                        replaceFragment(cecondFragment);
                    break;
                    case R.id.toshird:
                        initColor(v);
                        replaceFragment(thirdFragment);
                    break;
                }

            }
        };
        tofirst.setOnClickListener(listener);
        tosecond.setOnClickListener(listener);
        toshird.setOnClickListener(listener);

    }

    private void initView() {
        tofirst=findViewById(R.id.tofirst);
        tosecond=findViewById(R.id.tosecond);
        toshird=findViewById(R.id.toshird);
        initColor(tofirst);
    }
    private void initColor(View view){
        tofirst.setBackgroundColor(Color.parseColor("#00FF00"));
        tosecond.setBackgroundColor(Color.parseColor("#EE8262"));
        toshird.setBackgroundColor(Color.parseColor("#00F5FF"));
        view.setBackgroundColor(Color.parseColor("#FFFFF0"));
}
          //传入一个参数
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager=getSupportFragmentManager();//一个事物管理者，获取一个值
        FragmentTransaction transaction=fragmentManager.beginTransaction();//提交一个事务transaction
        defFragment=fragment;//传入的Fragment赋值给def
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();

    }
}
